﻿
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using DataContextLayer;
using System;
using System.Linq.Expressions;

namespace BlueHarvestSnippetGenerator.Pages
{
    
   

	public partial class Welcome : Page
	{

	    private enum UserDisplayView
	    {
	        Login,
	        Register
	    };

        public bool ChkRegister
        {
            get { return chkRegister.IsChecked != null && chkRegister.IsChecked.Value; }
            set { chkRegister.IsChecked = value; }
        }
        

	    private UserDisplayView _userDisplayView;

	    public LoginHandler LoginHandler{ get; private set; }

	    public RegistrationHandler RegistrationHandler{ get; private set; }

	    public Dictionary<string, string> FormDataInfo { get; private set; }

		public Welcome()
		{

			InitializeComponent();

            _userDisplayView = UserDisplayView.Login;

            LoginHandler = new LoginHandler();
            
            RegistrationHandler = new RegistrationHandler();
           
		}
        
     
        private void BtnLoginClick(object sender, RoutedEventArgs e)
        {
            SubmitFormData();

            if (ChkRegister)
            {
                RegisterUserSubmit();
            }
            else
            {
                LoginUserSubmit();
            }
           
        }

        private void RegisterUserSubmit()
        {
            if (RegistrationHandler.RegisterUserInformation())
            {
                DisplayMessage(RegistrationHandler.GetMessage());
            }
            else
            {
                DisplayMessage(RegistrationHandler.GetMessage());
            }
        }


        private void LoginUserSubmit()
        {
            if (LoginHandler.LogUserIn())
            {
                Application.Current.Properties["userId"] = LoginHandler.GetUserId();
                NavigationService.PageNav(PageName.UserProfilePage);
               
            }
            else
            {
                DisplayMessage(LoginHandler.GetMessage());
            }
           
        }

        private void SubmitFormData()
        {
            FormDataInfo = new Dictionary<string, string>();

            foreach (var textBox in Utilitys.GetAllControls<TextBox>(this)
            .Where(textBox => textBox.Visibility == Visibility.Visible))
            {
                FormDataInfo.Add(textBox.Name,textBox.Text.Trim());
            }

            if (ChkRegister)
            {
                RegistrationHandler.GetUserValues(FormDataInfo);
            }
            else
            {
                LoginHandler.GetUserValues(FormDataInfo);
            }
           
        }

        private void DisplayMessage(string msg)
	    {
            MessageBox.Show(msg);
	    }

        private void RegisterChecked(object sender, RoutedEventArgs e)
        {
            _userDisplayView = UserDisplayView.Register;

            CurrentView(_userDisplayView);
        }
       
        private void RegisterUnchecked(object sender, RoutedEventArgs e)
        {
            _userDisplayView = UserDisplayView.Login;

            CurrentView(_userDisplayView);
        }

        private void CurrentView(UserDisplayView loginStatus)
        {
            ResetAllTxtBoxes();

            btnLogin.Content = _userDisplayView.ToString();

            switch (loginStatus)
            {
                case UserDisplayView.Login:
                    LoginView();
                    break;
                case UserDisplayView.Register:
                    RegisterView();
                    break;
            }
        }
    
        private void RegisterView()
        {
            txtUserEmail.Visibility = Visibility.Visible;
            txbUserEmail.Visibility = Visibility.Visible;
            txtUserPassword.Visibility = Visibility.Visible;
            txbUserPassword.Visibility = Visibility.Visible;
            txtUserName.Visibility = Visibility.Visible;
            txbUserName.Visibility = Visibility.Visible;
            ChkRegister = true;
        }
       
        private void LoginView()
        {
            txtUserEmail.Visibility = Visibility.Hidden;
            txbUserEmail.Visibility = Visibility.Hidden;
            txtUserPassword.Visibility = Visibility.Visible;
            txtUserName.Visibility = Visibility.Visible;
            txbUserPassword.Visibility = Visibility.Visible;
            txbUserName.Visibility = Visibility.Visible;
            ChkRegister = false;
        }
        
        private void ResetAllTxtBoxes()
        {
            foreach (var textBox in Utilitys.GetAllControls<TextBox>(this))
            {
                textBox.Clear();
            }

        }

    }
}